package com.group.three.controller.order;


import com.group.three.entity.OrderDetailVO;
import com.group.three.entity.OrderSimpleVO;
import com.group.three.service.OrderService;
import com.group.three.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.Valid;

import java.util.List;
import java.util.Objects;

/**
 * 店铺端,订单接口
 *
 * @author Chopper
 * @since 2020/11/17 4:35 下午
 **/
@Slf4j
@RestController
@RequestMapping("/store/order/order")
@Api(tags = "店铺端,订单接口")
public class OrderStoreController {

    /**
     * 订单
     */
    @Autowired
    private OrderService orderService;


    @ApiOperation(value = "查询订单列表")
    @GetMapping
    public JsonResult queryMineOrder(int pageNum, int pageSize) {
        List<OrderSimpleVO> orderItem = orderService.queryByParams(pageNum, pageSize);
        if (orderItem != null) {
            return new JsonResult("200", "订单列表查询成功", orderItem);
        }
        return new JsonResult("400", "订单列表查询失败");
    }

    @ApiOperation(value = "订单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单编号", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/{orderSn}")
    public JsonResult detail(@NotNull @PathVariable String orderSn) {
        List<OrderDetailVO> orderDetails = orderService.getBySn(orderSn);

        if (orderDetails != null) {
            return new JsonResult("200", "订单详情查询成功", orderDetails);
        }
        return new JsonResult("400", "订单详情查询成功");

    }


    @ApiOperation(value = "修改订单价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单sn", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "orderPrice", value = "订单价格", required = true, dataType = "Double", paramType = "query"),
    })
    @PutMapping(value = "/update/{orderSn}/price")
    public JsonResult updateOrderPrice(@PathVariable String orderSn,
                                       @RequestParam Double orderPrice) {
        int i = orderService.updatePrice(orderSn, orderPrice);

        if (i == 1) {
            return new JsonResult("200", "订单价格修改成功");
        }
        return new JsonResult("400", "订单价格修改失败");
    }


    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单编号", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "reason", value = "取消原因", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/{orderSn}/cancel")
    public JsonResult cancel(@PathVariable String orderSn, @RequestParam String reason) {
        int i = orderService.cancelOrder(orderSn, reason);
        if (i == 1) {
            return new JsonResult("200", "订单取消成功");
        }
        return new JsonResult("400", "订单取消失败");
    }

    @ApiOperation(value = "修改收件地址")
    @PostMapping("/update/{orderSn}/consignee")
    public JsonResult consignee(@PathVariable("orderSn") String orderSn,
                                @RequestParam(name = "name") String name,
                                @RequestParam(name = "mobile") String mobile,
                                @RequestParam(name = "address") String address
    ) {
        int i = orderService.updateMemberAddress(orderSn, name, mobile, address);
        if (i == 1) {
            return new JsonResult("200", "修改地址成功");
        }
        return new JsonResult("400", "修改地址失败");
    }


    @ApiOperation(value = "订单发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderSn", value = "订单sn", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "logisticsNo", value = "发货单号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "logisticsId", value = "物流公司", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/{orderSn}/delivery")
    public JsonResult delivery(@NotNull(message = "参数非法") @PathVariable String orderSn,
                               @NotNull(message = "发货单号不能为空") String logisticsNo,
                               @NotNull(message = "请选择物流公司") String logisticsId) {
        int i = orderService.delivery(orderSn, logisticsNo, logisticsId);
        if (i == 1) {
            return new JsonResult("200", "订单发货成功");
        }
        return new JsonResult("400", "订单发货失败");
    }
}